<?php 
	Class Hotelroom extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('model_hotelroom');	
			$this->load->model('model_hotel');	
		}	
		
		public function index(){
			if(isset($_GET['hotelid'])){
				$data['hotel']=$this->model_hotel->getdata(intval($_GET['hotelid']));				
				$data['data']=$this->model_hotelroom->getallroombyhoteid(intval($_GET['hotelid']));
			}else $data['data']=$this->model_hotelroom->listall(200000,0);
			
			$data['template']='hotel/hotel_room';
			$this->load->view('layout',$data);
		}
		
		public function edit(){
			if(isset($_GET["id"])) $data['detail']=$this->model_hotelroom->getdata(intval($_GET["id"]));	
			else $data['detail']=NULL;
			$data['hotel']=$this->model_hotel->getdata(intval($_GET['hotelid']));
			
			$data['template']='hotel/hotelroom_edit';
			$this->load->view('layout',$data);
		}
		
		public function insert(){
			$data['name']=$_POST['name'];
			$data['maxperson']=$_POST['maxperson'];	
			$data['numberroom']=$_POST['numberroom'];
			$data['description']=$_POST['description'];
			$data['facilities']=$_POST['facilities'];
			$data['extrabed']=$_POST['extrabed'];
			$data['conditions']=$_POST['conditions'];
			$data['type']=$_POST['type'];
			$data['status']=$_POST['status'];
			$data['hotelid']=$_GET['hotelid'];
            if($_FILES['fuavatar']['name'] != NULL){
                $config['upload_path'] = './uploads/hotelroom';
                $config['allowed_types'] = '*';
                $config['max_size']	= '3000';
                $config['max_width']  = '2704';
                $config['max_height']  = '2700';
                $this->load->library('upload',$config);
                if(!$this->upload->do_upload("fuavatar")){
                    header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
                    return FALSE;
                }else{
                    $dataimg = $this->upload->data();
                    $data['avatar'] = $dataimg['file_name'];
                }
            }
			$this->model_hotelroom->add($data);
			header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
		}
		
		public function update(){
			if(isset($_GET["id"])){
				$data['name']=$_POST['name'];
				$data['maxperson']=$_POST['maxperson'];	
				$data['numberroom']=$_POST['numberroom'];
				$data['description']=$_POST['description'];
				$data['facilities']=$_POST['facilities'];
	  			$data['extrabed']=$_POST['extrabed'];
	  			$data['conditions']=$_POST['conditions'];
				$data['type']=$_POST['type'];
				$data['status']=$_POST['status'];
				$data['hotelid']=$_GET['hotelid'];
                if($_FILES['fuavatar']['name'] != NULL){
                    $config['upload_path'] = './uploads/hotelroom';
                    $config['allowed_types'] = '*';
                    $config['max_size']	= '3000';
                    $config['max_width']  = '2704';
                    $config['max_height']  = '2700';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("fuavatar")){
                        header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
                        return FALSE;
                    }else{
                        $dataimg = $this->upload->data();
                        $data['avatar'] = $dataimg['file_name'];
                    }
                }
				$this->model_hotelroom->update($data,intval($_GET['id']));
				header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
			}	
		}
		
		public function delete(){
			if(isset($_GET["id"])){
                $data = $this->model_hotelroom->getdata($_GET['id']);
                @unlink("uploads/hotelroom/".$data['avatar']);
				$this->model_hotelroom->del($data['id']);	
				header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
			}	
		}
		
		public function changestatus(){	
			if(isset($_GET["status"])){
				$data['status']=intval($_GET['status']);
				$this->model_hotelroom->update($data,intval($_GET["id"]));
				header('Location: '.base_url().'admin/hotelroom/index?hotelid='.$_GET['hotelid']);
			}
		}
        public function createThumbnail($fileName,$path,$width,$height){
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = 'uploads/'.$path.'/'.$fileName;
            $config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['thumb_marker'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
	}
?>